﻿using UnityEngine;
using System.Collections;
using UnityEngine.Assertions;
using System.Collections.Generic;

[behaviac.TypeMetaInfo("GameScene", "GameScene -> Agent")]
public class GameScene : behaviac.Agent {

    private static GameScene _instance = null;
    public static GameScene instance {
        get {
            return _instance;
        }
    }

    private GameData _gameData = null;
    public GameData gameData {
        get {
            if (this._gameData == null)
                this._gameData = new GameData();
            return this._gameData;
        }
    }

    private BulletMgr _bulletMgr = null;

    private Tank _tank = null;
    public Tank tank {
        get {
            return _tank;
        }
    }
    public void Awake() {
        base.Init();
        _instance = this;

        Application.runInBackground = true;
        this.gameData.Init();
        this._bulletMgr = new BulletMgr();
        this._bulletMgr.Init();

        BehaviacSystem.instance.Init();
        behaviac.Agent.BindInstance(GameScene.instance, "gameScene");
        behaviac.Config.SocketPort = 45612;
    }


    public void Start() {

        GameObject tankGameObj = Resources.Load("Prefabs/Tank") as GameObject;
        GameObject tankObj = Instantiate(tankGameObj) as GameObject;
        _tank = tankObj.GetComponent<Tank>();
        _tank.Init();
        _tank.transform.position = Vector3.zero;
        _tank.transform.parent = this.transform;

        GameObject modality = MapExamples.Modality_01();
        modality.transform.parent = this.transform;
    }

    public void OnGUI() {
        if (GUI.Button(new Rect(10, Screen.height - 60, 150, 50), "Camera")) {
            Debug.Log("camera button");
            CameraMgr.instance.CameraChangeNext();
        }
    }

    public void Update() {
        this.gameData.tickTime = GameData.GAME_TICK_TIME;
        behaviac.Workspace.Instance.Update();
        this.Tick();

    }

    public void Tick() {
        CameraMgr.instance.Tick();

        if (_bulletMgr != null)
            _bulletMgr.Tick();


        if (this._tank) {
            this._tank.Tick();
        }

        this.gameData.Tick();
    }


    public bool AddBullet(Bullet bullet) {
        if (_bulletMgr == null) {
            return false;
        }
        return _bulletMgr.AddBullet(bullet);
    }

    public GameObject SpawnEffect(string fileName, Transform parent, Vector3 pos, float sc) {
        if (parent == null) return null;
        GameObject obj = Resources.Load(fileName) as GameObject;
        GameObject effectObj = GameObject.Instantiate(obj) as GameObject;
        effectObj.transform.parent = parent;
        effectObj.transform.localPosition = pos;
        effectObj.transform.localScale = new Vector3(sc, sc, sc);
        return effectObj;
    }


}